import {ref} from "vue"

/*
* 人物的等级和经验
* */
export interface UserLevelType {
    level: number,  // 等级
    name: string, // 境界名称
    exp: number // 需要的经验
}


export const userLevelList: Array<UserLevelType> = [
    {level: 1, name: "练气期", exp: 500},         // 基础修行
    {level: 2, name: "筑基期", exp: 1500},        // +1000
    {level: 3, name: "金丹", exp: 3500},        // +2000
    {level: 4, name: "元婴", exp: 7500},        // +4000
    {level: 5, name: "分神", exp: 15000},       // +7500
    {level: 6, name: "合体", exp: 30000},       // +15000
    {level: 7, name: "悟道", exp: 60000},       // +30000
    {level: 8, name: "千劫", exp: 120000},      // +60000
    {level: 9, name: "大乘", exp: 250000},        // +130000
    {level: 10, name: "飞升境", exp: 1000000}       // +750000
]


export const useUserInfoStore = defineStore('userinfoStore', () => {
    // 用户等级信息
    const userinfo = ref({
        expAll: 0,
        level: 1,
        name: "",
    })
    // 当前升级需要的经验值和拥有的经验值
    const nextExp = ref({
        current: 0,
        all: 0,
    })
    // 用户的灵石
    const lingshiNum = ref(0)
    // 增加经验
    const addExp = (exp: number) => {
        userinfo.value.expAll += exp;
        getLevel()
    }
    // 根据当前经验获取等级
    const getLevel = () => {
        let exp = 0
        for (let i = 0; i < userLevelList.length; i++) {
            exp += userLevelList[i].exp
            if (userinfo.value.expAll < exp || i === userLevelList.length - 1) {
                userinfo.value.level = userLevelList[i].level
                userinfo.value.name = userLevelList[i].name
                nextExp.value.all = userLevelList[i].exp
                nextExp.value.current = userinfo.value.expAll - (exp - userLevelList[i].exp)
                break
            }
        }
    }
    // 增加灵石
    const addLingshi = (num: number) => {
        lingshiNum.value += num
    }
    // 支出灵石
    const payLingshi = (num: number) => {
        if (lingshiNum.value >= num) {
            lingshiNum.value -= num
            return true
        } else {
            return false
        }
    }
    // 初始化
    const initFunc = (exp: number = 0, lsNum: number = 0) => {
        addExp(exp)
        lingshiNum.value = lsNum
    }

    return {
        userinfo,
        nextExp,
        addExp,
        initFunc,
        lingshiNum,
        addLingshi,
        payLingshi,
    }
})